package com.http.intg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.http.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

public class HttpServiceClient {
    public static final Logger log = LoggerFactory.getLogger(HttpServiceClient.class);

    public static final String GET_HTTP_RESOURCE_PATH = "/queryUserById/{userid}";
    public static final String GET_HTTP_RESOURCE_PATH1 = "/queryUserList?pageNow={pageNow}&pageSize={pageSize}";
    public static final String GET_HTTP_RESOURCE_PATH2 = "/updateUser/{id}";
    public static final String GET_HTTP_RESOURCE_PATH3 = "/createUser";
    public static final String GET_HTTP_RESOURCE_PATH4 = "/deleteUser/{id}";

    private RestTemplate restTemplate;

    public String getUserById(int userid) throws IOException {
        String response = null;
        System.out.println("Test url:"+HttpServiceClient.GET_HTTP_RESOURCE_PATH);
        try{
            response = this.restTemplate.getForObject(HttpServiceClient.GET_HTTP_RESOURCE_PATH,String.class,userid);
        }catch (Exception e){
            e.printStackTrace();
        }

        log.info("response: "+ response);
        JSONObject responseBody = JSON.parseObject(response);
        log.info("responseBody: "+responseBody);
        String message = responseBody.getString("message");
        log.info("message: "+message);
        String user = responseBody.getString("object");
        log.info("user: "+user);
        log.info("Test boolean: "+StringUtils.isNotBlank(user));
        String username = null;
        if(StringUtils.isNotBlank(user)) {
            JSONObject jsonuser = JSON.parseObject(user);
            log.info("jsonuser: " + jsonuser);
            username = jsonuser.getString("username");
        }
        return username;
    }

    public String getUserList(int pageNow, int pageSize) throws IOException{
        ResponseEntity<String> response = null;
//        方法1：使用getForEntity方法
        response = this.restTemplate.getForEntity(HttpServiceClient.GET_HTTP_RESOURCE_PATH1,String.class,pageNow,pageSize);
//        方法2：使用exchange方法
//        HttpEntity<String> requestEntity = null;
//        response = this.restTemplate.exchange(HttpServiceClient.GET_HTTP_RESOURCE_PATH1,HttpMethod.GET,requestEntity,String.class,pageNow,pageSize);
        log.info("response: "+ response);
        JSONObject responseBody = JSON.parseObject(response.getBody());
        log.info("responseBody: "+responseBody);
        String message = responseBody.getString("message");
        log.info("message: "+message);
        String user = responseBody.getString("object");
        log.info("user: "+user);
        return user;
    }

    public String updateUser(int id, String token, User user) throws IOException{
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=UTF-8");
        headers.add("token",token);
        String body = JSON.toJSONString(user);
        log.info("body:"+body);
        HttpEntity<String> requestEntity = new HttpEntity<>(body,headers);
        ResponseEntity<String> response = null;
//        方法1：使用postForEntity方法
        response = this.restTemplate.postForEntity(HttpServiceClient.GET_HTTP_RESOURCE_PATH2,requestEntity,String.class,id);
//        方法2：使用exchange方法
//        response = this.restTemplate.exchange(HttpServiceClient.GET_HTTP_RESOURCE_PATH2,HttpMethod.POST,requestEntity,String.class,id);
        log.info("response: "+ response);
        JSONObject responseBody = JSON.parseObject(response.getBody());
        log.info("responseBody: "+responseBody);
        String message = responseBody.getString("message");
        log.info("message: "+message);
        String result = responseBody.getString("object");
        log.info("result: "+result);
        return result;
    }

    public String createUser(User user) throws IOException{
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset=UTF-8");
        String body = JSON.toJSONString(user);
        log.info("body:"+body);
        HttpEntity<String> requestEntity = new HttpEntity<>(body,headers);
//        方法1：使用postForObject方法
        String response = null;
        response = this.restTemplate.postForObject(HttpServiceClient.GET_HTTP_RESOURCE_PATH3,requestEntity,String.class);
        log.info("response: "+ response);
        JSONObject responseBody = JSON.parseObject(response);
//        方法2：使用exchange方法
//        ResponseEntity<String> response = null;
//        response = this.restTemplate.exchange(HttpServiceClient.GET_HTTP_RESOURCE_PATH3,HttpMethod.POST,requestEntity,String.class);
//        log.info("response: "+ response);
//        JSONObject responseBody = JSON.parseObject(response.getBody());
//        log.info("responseBody: "+responseBody);
        String message = responseBody.getString("message");
        log.info("message: "+message);
        String result = responseBody.getString("object");
        log.info("result: "+result);
        return result;
    }

    public String deleteUser(int id) throws IOException{
        HttpEntity<String> requestEntity = null;
        ResponseEntity<String> response = null;
        response = this.restTemplate.exchange(HttpServiceClient.GET_HTTP_RESOURCE_PATH4,HttpMethod.DELETE,requestEntity,String.class,id);
        log.info("response: "+ response);
        JSONObject responseBody = JSON.parseObject(response.getBody());
        log.info("responseBody: "+responseBody);
        String message = responseBody.getString("message");
        log.info("message: "+message);
        String result = responseBody.getString("object");
        log.info("result: "+result);
        return result;
    }

    /**
     * @Description: The restTemplate to set
     * @param restTemplate
     */
    public void setRestTemplate(final RestTemplate restTemplate){this.restTemplate = restTemplate;}

}
